$(function () {
    $("#jqGrid").Grid({
        url: '../incomecoin/list',
        colModel: [
			{label: 'id', name: 'id', index: 'id', key: true, hidden: true},
			{label: '交易编号', name: 'tradeNo', index: 'trade_no', width: 80},
			{label: '邀请人', name: 'uid', index: 'uid', width: 80},
			{label: '邀请人手机', name: 'phone', index: 'phone', width: 80},
			{label: '币数量', name: 'sportCoin', index: 'sportCoin', width: 80},
			{label: '兑换距离(米)', name: 'distance', index: 'distance', width: 80},
			{label: '被邀请人编号', name: 'inviteUid', index: 'inviteUid', width: 50},
			{label: '被邀请人手机号', name: 'invitePhone', index: 'invitePhone', width: 60},
			{label: '获取类型', name: 'type', index: 'type', width: 50,formatter(value){
			    if(value==1){
			        return '普通邀请注册';
                }else if(value==2){
                    return '普通运动兑换';
                }else if(value==3){
                    return '<span class="label-success">D-one产品</span>';
                }else if(value==4){
                    return 'Done运动兑换';
                }else if(value==5){
                    return '新用户注册';
                }else if(value==6){
                    return 'Done邀请注册';
                }else if(value==7){
                    return '任务奖励';
                }else if(value==8){
                    return '兑换产品';
                }else if(value==11){
                    return '运动挖矿';
                }else if(value==10){
                    return 'D-one产品';
                }else {
			        return '其他';
                }
                }
            },
			{label: '状态', name: 'statu', index: 'statu', width: 30,formatter(value){
			    if(value==1){
			        return '<span> 有效</span>';
                }
                return '<span>无效</span>';
                }},
			{label: '创建时间', name: 'createdDate', index: 'created_date', width: 60}]
    });
});

let vm = new Vue({
	el: '#rrapp',
	data: {
        showList: true,
        title: null,
		incomecoin: {},
		ruleValidate: {
			name: [
				{required: true, message: '名称不能为空', trigger: 'blur'}
			]
		},
		q: {
		    name: '',
            mobile: '',
            type: '',
            coins:'',
            uid:''
		}
	},
	methods: {
		query: function () {
			vm.reload();
		},
		add: function () {
			vm.showList = false;
			vm.title = "新增";
			vm.incomecoin = {};
		},
		update: function (event) {
            let id = getSelectedRow("#jqGrid");
			if (id == null) {
				return;
			}
			vm.showList = false;
            vm.title = "修改";

            vm.getInfo(id)
		},
		saveOrUpdate: function (event) {
            let url = vm.incomecoin.id == null ? "../incomecoin/save" : "../incomecoin/update";
            Ajax.request({
			    url: url,
                params: JSON.stringify(vm.incomecoin),
                type: "POST",
			    contentType: "application/json",
                successCallback: function (r) {
                    alert('操作成功', function (index) {
                        vm.reload();
                    });
                }
			});
		},
		del: function (event) {
            let ids = getSelectedRows("#jqGrid");
			if (ids == null){
				return;
			}
			confirm('确定要删除选中的记录？', function () {
                Ajax.request({
				    url: "../incomecoin/delete",
                    params: JSON.stringify(ids),
                    type: "POST",
				    contentType: "application/json",
                    successCallback: function () {
                        alert('操作成功', function (index) {
                            vm.reload();
                        });
					}
				});
			});
		},
		getInfo: function(id){
            Ajax.request({
                url: "../incomecoin/info/"+id,
                async: true,
                successCallback: function (r) {
                    vm.incomecoin = r.incomecoin;
                }
            });
		},
		reload: function (event) {
			vm.showList = true;
            let page = $("#jqGrid").jqGrid('getGridParam', 'page');
			$("#jqGrid").jqGrid('setGridParam', {
                postData: {'name': vm.q.name,'mobile': vm.q.mobile,"type":vm.q.type,'uid':vm.q.uid},
                page: page
            }).trigger("reloadGrid");
            vm.handleReset('formValidate');
            vm.queryCoin();
		},
        reloadSearch: function() {
            vm.q = {
                name: '',
                mobile: '',
                type: '',
                coins:'',
                uid:''
            }
            vm.reload();
        },
        handleSubmit: function (name) {
            handleSubmitValidate(this, name, function () {
                vm.saveOrUpdate()
            });
        },
        handleReset: function (name) {
            handleResetForm(this, name);
        },
        queryCoin: function () {
            Ajax.request({
                url: "../incomecoin/coins?name="+vm.q.name+"&mobile="+vm.q.mobile+"&uid="+vm.q.uid+"&type="+vm.q.type,
                async: true,
                successCallback: function (r) {
                    console.log(r.coins);
                    vm.q.coins = r.coins;
                }
            });
        }
	}
});